import React from "react";
import axios from "axios";
import sha512 from "crypto-js/sha512";

export default class Login extends React.Component {
  state = {
    name    : "",
    password: ""
  };
  nameChange = e => {
    this.setState({
      name: e.target.value
    });
    console.log("name", this.state);
  };
  passwordChange = e => {
    this.setState({
      password: e.target.value
    });
  };
  submit = success => {
    console.log("submit", this.state);
    var data           = this.state;
        data.telephone = data.password;
    delete data.password;

    data.telephone = sha512(data.telephone).words.join("");
    axios({
      url   : "http://134.175.39.187:3000/users/login",
      method: "post",
      data  : data
    }).then(res => {
      success(true);
    });
  };
  render() {
    const { boo, success, fail } = this.props;
    console.log(boo);
    console.log(this.state);
    if (!boo) {
      return (
        <div>
          <input type="text" onChange={this.nameChange} />
          <br />
          <input type="password" onChange={this.passwordChange} />

          <button onClick={() => this.submit(success)}>提交</button>
        </div>
      );
    } else {
      return (
        <div>
          <p>欢迎登录</p>
          <button onClick={() => fail(false)}>退出</button>
        </div>
      );
    }
  }
}
